package com.cmwa.solution.business.customerEquity.prdFeeYield.service.impl;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.customerEquity.prdFeeYield.dao.PrdFeeYieldDao;
import com.cmwa.solution.business.customerEquity.prdFeeYield.entity.FeeYieldVo;
import com.cmwa.solution.business.customerEquity.prdFeeYield.entity.PrdFeeYieldVo;
import com.cmwa.solution.business.customerEquity.prdFeeYield.service.PrdFeeYieldService;
import com.cmwa.solution.business.product.entity.TreFeeDetailVo;
import com.cmwa.solution.business.product.entity.TreFeeRateChangeVo;
import com.cmwa.solution.business.product.entity.TreFeeTypeVo;
import com.cmwa.solution.utils.FmtMicrometer;
import com.cmwa.solution.utils.SessionUtils;

import net.sf.json.JSONArray;

@Service
public class PrdFeeYieldServiceImpl implements PrdFeeYieldService{

	@Resource
	private PrdFeeYieldDao prdFeeYieldDao;

	@Override
	public Page<PrdFeeYieldVo> prdFeeYieldListPage(SearchParam sp) {
		// TODO Auto-generated method stub
		if(StringUtils.equals(SessionUtils.getEmployee().getEmpType(), "OUT")) {
			sp.getSp().put("empId", SessionUtils.getEmployee().getID());
		}
		List<PrdFeeYieldVo> items  = prdFeeYieldDao.prdFeeYieldListPage(sp);
		for(PrdFeeYieldVo vo : items){
			if(StringUtils.isNotBlank(vo.getSubPrdCode())) {
				SearchParam sp2 = sp;
				sp2.getSp().put("subPrdCode", vo.getSubPrdCode());
				PrdFeeYieldVo vo2 = prdFeeYieldDao.queryExpectPayAmountAndBnsAmt(sp2);
				
				if(StringUtils.isNotBlank(vo2.getExpectPayAmount())){
					vo.setExpectPayAmount(FmtMicrometer.fmtMicrometer(vo2.getExpectPayAmount()));
				}
				if(StringUtils.isNotBlank(vo2.getBnsAmt())){
					vo.setBnsAmt(FmtMicrometer.fmtMicrometer(vo2.getBnsAmt()));
				}
			}else {
				vo.setExpectPayAmount("0.00");
				vo.setBnsAmt(FmtMicrometer.fmtMicrometer("0.00"));
			}
		}
		return Page.create(items, sp.getStart(), sp.getLimit(), sp.getTotal());
	}

	@Override
	public List<PrdFeeYieldVo> prdFeeYieldList(SearchParam sp) {
		// TODO Auto-generated method stub
		if(StringUtils.equals(SessionUtils.getEmployee().getEmpType(), "OUT")) {
			sp.getSp().put("empId", SessionUtils.getEmployee().getID());
		}
		List<PrdFeeYieldVo> items  = prdFeeYieldDao.prdFeeYieldList(sp);
		for(PrdFeeYieldVo vo : items){
			if(StringUtils.isNotBlank(vo.getSubPrdCode())) {
				SearchParam sp2 = sp;
				sp2.getSp().put("subPrdCode", vo.getSubPrdCode());
				PrdFeeYieldVo vo2 = prdFeeYieldDao.queryExpectPayAmountAndBnsAmt(sp2);
				
				if(StringUtils.isNotBlank(vo2.getExpectPayAmount())){
					vo.setExpectPayAmount(FmtMicrometer.fmtMicrometer(vo2.getExpectPayAmount()));
				}
				if(StringUtils.isNotBlank(vo2.getBnsAmt())){
					vo.setBnsAmt(FmtMicrometer.fmtMicrometer(vo2.getBnsAmt()));
				}
			}else {
				vo.setExpectPayAmount("0.00");
				vo.setBnsAmt(FmtMicrometer.fmtMicrometer("0.00"));
			}
		}
		return items;
	}

	@Override
	public Page<FeeYieldVo> feeYieldListPage(SearchParam sp) {
		// TODO Auto-generated method stub
		List<FeeYieldVo> items  = prdFeeYieldDao.feeYieldListPage(sp);
		for(FeeYieldVo vo : items){
			if(StringUtils.isNotBlank(vo.getExpectPayAmount())){
				vo.setExpectPayAmount(FmtMicrometer.fmtMicrometer(vo.getExpectPayAmount()));
			}
			if(StringUtils.isNotBlank(vo.getBnsAmt())){
				vo.setBnsAmt(FmtMicrometer.fmtMicrometer(vo.getBnsAmt()));
			}
		}
		return Page.create(items, sp.getStart(), sp.getLimit(), sp.getTotal());
	}

	@Override
	public List<FeeYieldVo> feeYieldList(SearchParam sp) {
		// TODO Auto-generated method stub
		List<FeeYieldVo> items  = prdFeeYieldDao.feeYieldList(sp);
		for(FeeYieldVo vo : items){
			if(StringUtils.isNotBlank(vo.getExpectPayAmount())){
				vo.setExpectPayAmount(FmtMicrometer.fmtMicrometer(vo.getExpectPayAmount()));
			}
			if(StringUtils.isNotBlank(vo.getBnsAmt())){
				vo.setBnsAmt(FmtMicrometer.fmtMicrometer(vo.getBnsAmt()));
			}
		}
		return items;
	}

	@Override
	public FeeYieldVo queryYJandSJAmount(SearchParam sp) {
		// TODO Auto-generated method stub
		if(StringUtils.equals(SessionUtils.getEmployee().getEmpType(), "OUT")) {
			sp.getSp().put("empId", SessionUtils.getEmployee().getID());
		}
		return prdFeeYieldDao.queryYJandSJAmount(sp);
	}

	@Override
	public FeeYieldVo queryYJandSJCount(SearchParam sp) {
		// TODO Auto-generated method stub
		if(StringUtils.equals(SessionUtils.getEmployee().getEmpType(), "OUT")) {
			sp.getSp().put("empId", SessionUtils.getEmployee().getID());
		}
		return prdFeeYieldDao.queryYJandSJCount(sp);
	}

	@Override
	public Page<FeeYieldVo> queryFeeYieldBySubPrdCodeListPage(SearchParam sp) {
		List<FeeYieldVo> items = prdFeeYieldDao.queryFeeYieldBySubPrdCodeListPage(sp);
		return Page.create(items, sp.getStart(), sp.getLimit(), sp.getTotal());
	}
	
	@Override
	public Page<FeeYieldVo> queryChangFeeYieldByFeeIdListPage(SearchParam sp) {
		List<FeeYieldVo> items = prdFeeYieldDao.queryChangFeeYieldByFeeIdListPage(sp);
		return Page.create(items, sp.getStart(), sp.getLimit(), sp.getTotal());
	}

	@SuppressWarnings("unchecked")
	@Override
	public void prdFeeOrYieldAdd(FeeYieldVo vo) {
		// TODO Auto-generated method stub
		vo.setStatus("C");
		vo.setCreateId(SessionUtils.getEmployee().getID());
		String feeType=vo.getFeeType();
		
		String payList=vo.getPayList();
		List<FeeYieldVo> feeYieldVos;
		JSONArray feeYieldVosJSONArray = JSONArray.fromObject(payList);
		feeYieldVos = (List<FeeYieldVo>)JSONArray.toCollection(feeYieldVosJSONArray, FeeYieldVo.class);
		
		if(feeType.equals("FM01") || feeType.equals("FC01")) {
			prdFeeYieldDao.insertFee(vo);
			String feeId = vo.getFeeId();
			for(int i = 0;i<feeYieldVos.size();i++) {
				FeeYieldVo feeYieldVo=feeYieldVos.get(i);
				feeYieldVo.setFeeId(feeId);
				feeYieldVo.setCreateId(SessionUtils.getEmployee().getID());
				if(StringUtils.isNotBlank(feeYieldVo.getBnsDate())) {
					feeYieldVo.setHandleStatus("PROCESSED");
				}else {
					feeYieldVo.setHandleStatus("NA");
				}
				feeYieldVo.setSubPrdCode(vo.getSubPrdCode());
				prdFeeYieldDao.insertFeePlan(feeYieldVo);
			}
		}
		if(feeType.equals("PY99")) {
			prdFeeYieldDao.insertYield(vo);
			String feeId = vo.getFeeId();
			for(int i = 0;i<feeYieldVos.size();i++) {
				FeeYieldVo feeYieldVo=feeYieldVos.get(i);
				feeYieldVo.setFeeId(feeId);
				feeYieldVo.setCreateId(SessionUtils.getEmployee().getID());
				if(StringUtils.isNotBlank(feeYieldVo.getBnsDate())) {
					feeYieldVo.setHandleStatus("PROCESSED");
				}else {
					feeYieldVo.setHandleStatus("NA");
				}
				feeYieldVo.setSubPrdCode(vo.getSubPrdCode());
				prdFeeYieldDao.insertYieldPlan(feeYieldVo);
			}
		}
		if(feeType.equals("PZ99")) {
			prdFeeYieldDao.deleteReturnPlan(vo.getSubPrdCode());
			for(int i = 0;i<feeYieldVos.size();i++) {
				FeeYieldVo feeYieldVo=feeYieldVos.get(i);
				feeYieldVo.setFeeId(i+1+"");
				feeYieldVo.setCreateId(SessionUtils.getEmployee().getID());
				if(StringUtils.isNotBlank(feeYieldVo.getBnsDate())) {
					feeYieldVo.setHandleStatus("PROCESSED");
				}else {
					feeYieldVo.setHandleStatus("NA");
				}
				feeYieldVo.setSubPrdCode(vo.getSubPrdCode());
				prdFeeYieldDao.insertReturnPlan(feeYieldVo);
			}
		}
	}

	@Override
	public FeeYieldVo feeYield(SearchParam sp) {
		// TODO Auto-generated method stub
		return prdFeeYieldDao.feeYield(sp);
	}

	@Override
	public String vReturnPlan(SearchParam sp) {
		// TODO Auto-generated method stub
		return prdFeeYieldDao.vReturnPlan(sp);
	}

	@Override
	public void feeOrYieldInfoUpdate(FeeYieldVo vo) {
		// TODO Auto-generated method stub
		if(StringUtils.isNotBlank(vo.getBnsDate())) {
			vo.setHandleStatus("PROCESSED");
		}else {
			vo.setHandleStatus("NA");
		}
		prdFeeYieldDao.feeOrYieldInfoUpdate(vo);
	}

	@Override
	public void feeOrYieldInfoDelete(SearchParam sp) {
		// TODO Auto-generated method stub
		prdFeeYieldDao.feeOrYieldInfoDelete(sp);
	}
	
	@Override
	public void insertPrdFeeDetail(TreFeeDetailVo treFeeDetailVo) {
		String createId = SessionUtils.getEmployee().getID();
		treFeeDetailVo.setCreateId(createId);
		prdFeeYieldDao.insertPrdFeeDetail(treFeeDetailVo);
	}
	
	@Override
	public void insertPrdFeeRateChange(TreFeeRateChangeVo treFeeRateChangeVo) {
		String createId = SessionUtils.getEmployee().getID();
		treFeeRateChangeVo.setCreateId(createId);
		prdFeeYieldDao.insertPrdFeeRateChange(treFeeRateChangeVo);
	}

	@Override
	public void deletePrdFeeDetail(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			String[] split = ids.split(",");
			prdFeeYieldDao.deletePrdFeeDetail(Arrays.asList(split));
		}
	}

	@Override
	public List<TreFeeTypeVo> feeTypeList() {
		return prdFeeYieldDao.feeTypeList();
	}
}
